package deviceConfig

import (
	"context"
	"devops-super/internal/consts"
	"devops-super/internal/model/mymodel"
	"devops-super/internal/service"
	"devops-super/pb"
	"errors"
	"fmt"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/golang/protobuf/proto"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

// SetViDeviceMeasureConfig 设置振动设备采集配置
func (s *sDeviceConfig) SetViDeviceMeasureConfig(ctx context.Context, deviceNo string, copyDevices []string, config *mymodel.ViMeasureConfItem) (err error) {
	if config == nil {
		return nil
	}
	allSns := make([]string, 0)
	allSns = append(allSns, deviceNo)
	if copyDevices != nil {
		allSns = append(allSns, copyDevices...)
	}
	deviceInfo0, err := service.Device().GetByDeviceNo(ctx, deviceNo)
	if err != nil {
		return err
	}
	if deviceInfo0 == nil || deviceInfo0.Id == 0 {
		err = errors.New("device not exist")
		return err
	}
	aconfig := &pb.ViMeasureConfig{
		ViMeasureType:       config.ViMeasureType,
		ViMeasureFreq:       config.ViMeasureFreq,
		ViMeasureTimes:      config.ViMeasureTimes,
		ViMeasurePoints:     config.ViMeasurePoints,
		ViMeasureSleep:      config.ViMeasureSleep,
		ViFirLow:            config.ViFirLow,
		ViFirHigh:           config.ViFirHigh,
		ViFirAmp:            config.ViFirAmp,
		ViOffsetX:           float32(config.ViOffsetX),
		ViOffsetY:           float32(config.ViOffsetY),
		ViOffsetZ:           float32(config.ViOffsetZ),
		ViFactorX:           float32(config.ViFactorX),
		ViFactorY:           float32(config.ViFactorY),
		ViFactorZ:           float32(config.ViFactorZ),
		ViRange:             config.ViRange,
		ViWaveEnable:        config.ViWaveEnable,
		ViMeasureState:      true, //一直为true
		ViSystemTime:        uint32(gtime.Now().Unix()),
		ViConfigVersion:     uint32(gtime.Now().Unix()),
		ViSaveLoop:          uint32(config.ViSaveLoop),
		ViOffsetTemperature: float32(config.ViOffsetTemperature),
		ViRemoveAccX:        config.ViRemoveAccX,
		ViRemoveAccY:        config.ViRemoveAccY,
		ViRemoveAccZ:        config.ViRemoveAccZ,
	}
	if deviceInfo0.Type == 0 {
		for _, thisSn := range allSns {
			// 生成proto对象
			aeMsg := &pb.SensorMessage{
				NodeId:      thisSn,
				UnixTime:    uint32(gtime.Now().Unix()),
				Microsecond: int32(gtime.Now().Microsecond()),
				Msg: &pb.SensorMessage_ViMeasureConfig{
					ViMeasureConfig: aconfig,
				},
			}
			pData, err := proto.Marshal(aeMsg)
			if err != nil {
				liberr.ErrIsNil(ctx, err, fmt.Sprintf("%v", err))
			}
			err = service.Aeemqx().CommonAePublish(ctx, thisSn, consts.EmqxAeSendType4, pData, &mymodel.EmqxPublishOps{Retained: true, ConfigType: consts.DeviceConfTypeViMeasureConfig})
			if err != nil {
				continue
			}
		}
	} else if deviceInfo0.Type == 2 {
		thisSn := deviceNo
		// 生成proto对象
		aeMsg := &pb.SensorMessage{
			NodeId:      thisSn,
			UnixTime:    uint32(gtime.Now().Unix()),
			Microsecond: int32(gtime.Now().Microsecond()),
			Msg: &pb.SensorMessage_ViMeasureConfig{
				ViMeasureConfig: aconfig,
			},
			NodeIds: allSns,
		}
		pData, err := proto.Marshal(aeMsg)
		if err != nil {
			liberr.ErrIsNil(ctx, err, fmt.Sprintf("%v", err))
		}
		err = service.Aeemqx().CommonAePublish(ctx, thisSn, consts.EmqxAeSendType4, pData, &mymodel.EmqxPublishOps{Retained: true, ConfigType: consts.DeviceConfTypeViMeasureConfig})
		if err != nil {
			liberr.ErrIsNil(ctx, err, fmt.Sprintf("%v", err))
		}
	}
	return
}
